﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class Admin_NewsEdit : System.Web.UI.Page
{
    public int News_id;
    protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!IsPostBack)
        {
            //if (Session["admin"] == null)
            //{
                //Response.Redirect("../Default.aspx");
            //}
            //else
            //{
           

                switch (Convert.ToString(Session["NewsType"]))
                {
                    case "Notice":
                        List.Text = "重要通知列表";
                        List.NavigateUrl = "Admin_Notice.aspx";
                        News_Edit.Text = "重要通知编辑";
                        if (Session["OperationType"].ToString().Equals("Edit"))
                        {
                            OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
                            var News = from news in dc.News
                                       join news_category in dc.News_Categories
                                       on news.News_category equals news_category.Category_id
                                       where news_category.Category_name.Equals("重要通知") && news.News_id.Equals(Convert.ToInt32(Session["News_id"]))
                                       select new
                                       {
                                           标题 = news.Title,
                                           内容 = news.Content
                                        };
                            content.Value = Convert.ToString(News.ToArray().ElementAt(0).内容);
                            title.Value = Convert.ToString(News.ToArray().ElementAt(0).标题);
                            dc.Dispose();
                        }
                        break;
                    case "CollegeCondition":
                        List.Text = "校情统计列表";
                        List.NavigateUrl = "Admin_CollegeCondition.aspx";
                        News_Edit.Text = "校情统计编辑";
                        if (Session["OperationType"].ToString().Equals("Edit"))
                        {
                            OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
                            var News = from news in dc.News
                                       join news_category in dc.News_Categories
                                       on news.News_category equals news_category.Category_id
                                       where news_category.Category_name.Equals("校情统计") && news.News_id.Equals(Convert.ToInt32(Session["News_id"]))
                                       select new
                                       {
                                           标题 = news.Title,
                                           内容 = news.Content
                                       };
                            content.Value = Convert.ToString(News.ToArray().ElementAt(0).内容);
                            title.Value = Convert.ToString(News.ToArray().ElementAt(0).标题);
                            dc.Dispose();
                        }
                        break;
                    case "Law":
                        List.Text = "政策法规列表";
                        List.NavigateUrl = "Admin_Law.aspx";
                        News_Edit.Text = "政策法规编辑";
                        if (Session["OperationType"].ToString().Equals("Edit"))
                        {
                            OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
                            var News = from news in dc.News
                                       join news_category in dc.News_Categories
                                       on news.News_category equals news_category.Category_id
                                       where news_category.Category_name.Equals("政策法规") && news.News_id.Equals(Convert.ToInt32(Session["News_id"]))
                                       select new
                                       {
                                           标题 = news.Title,
                                           内容 = news.Content
                                       };
                            content.Value = Convert.ToString(News.ToArray().ElementAt(0).内容);
                            title.Value = Convert.ToString(News.ToArray().ElementAt(0).标题);
                            dc.Dispose();
                        }
                        break;
                    case "Meeting":
                        List.Text = "一周会议列表";
                        List.NavigateUrl = "Admin_Meeting.aspx";
                        News_Edit.Text = "一周会议编辑";
                        if (Session["OperationType"].ToString().Equals("Edit"))
                        {
                            OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
                            var News = from news in dc.News
                                       join news_category in dc.News_Categories
                                       on news.News_category equals news_category.Category_id
                                       where news_category.Category_name.Equals("一周会议") && news.News_id.Equals(Convert.ToInt32(Session["News_id"]))
                                       select new
                                       {
                                           标题 = news.Title,
                                           内容 = news.Content
                                       };
                            content.Value = Convert.ToString(News.ToArray().ElementAt(0).内容);
                            title.Value = Convert.ToString(News.ToArray().ElementAt(0).标题);
                            dc.Dispose();
                        }
                        break;
                    case "Orgnization":
                        List.Text = "组织机构列表";
                        List.NavigateUrl = "Admin_Orgnization.aspx";
                        News_Edit.Text = "组织机构编辑";
                        if (Session["OperationType"].ToString().Equals("Edit"))
                        {
                            OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
                            var News = from news in dc.News
                                       join news_category in dc.News_Categories
                                       on news.News_category equals news_category.Category_id
                                       where news_category.Category_name.Equals("组织机构") && news.News_id.Equals(Convert.ToInt32(Session["News_id"]))
                                       select new
                                       {
                                           标题 = news.Title,
                                           内容 = news.Content
                                       };
                            content.Value = Convert.ToString(News.ToArray().ElementAt(0).内容);
                            title.Value = Convert.ToString(News.ToArray().ElementAt(0).标题);
                            dc.Dispose();
                        }
                        break;
                    case "Rule":
                        List.Text = "规章制度列表";
                        List.NavigateUrl = "Admin_Rule.aspx";
                        News_Edit.Text = "规章制度编辑";
                        if (Session["OperationType"].ToString().Equals("Edit"))
                        {
                            OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
                            var News = from news in dc.News
                                       join news_category in dc.News_Categories
                                       on news.News_category equals news_category.Category_id
                                       where news_category.Category_name.Equals("规章制度") && news.News_id.Equals(Convert.ToInt32(Session["News_id"]))
                                       select new
                                       {
                                           标题 = news.Title,
                                           内容 = news.Content
                                       };
                            content.Value = Convert.ToString(News.ToArray().ElementAt(0).内容);
                            title.Value = Convert.ToString(News.ToArray().ElementAt(0).标题);
                            dc.Dispose();
                        }
                        break;
                    case "Document":
                        List.Text = "校发文件列表";
                        List.NavigateUrl = "Admin_Document.aspx";
                        News_Edit.Text = "校发文件编辑";

                        ///OfficeDataClassesDataContext DC = new OfficeDataClassesDataContext();
                        /////var categories = from category in DC.Document_Categories
                         ///                select category.Category_name;
                        ///Document_categroy.DataSource = categories;
                        ///Document_categroy.DataBind();


                        if (Session["OperationType"].ToString().Equals("Edit"))
                        {
                            OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
                            var News = from news in dc.Documents
                                       where news.Document_id.Equals(Convert.ToInt32(Session["News_id"]))
                                       select new
                                       {
                                           标题 = news.Title,
                                           内容 = news.Content
                                       };
                            content.Value = Convert.ToString(News.ToArray().ElementAt(0).内容);
                            title.Value = Convert.ToString(News.ToArray().ElementAt(0).标题);
                            dc.Dispose();
                        }
                        break;
                //}
            }
        }

    }
    protected void Logoff_Click(object sender, EventArgs e)
    {
        Session.Remove("admin");
        Response.Redirect("../Default.aspx");
    }
    protected void NoticeManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Notice.aspx");
    }
    protected void DocumentCategoryManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_DocumentCategory.aspx");
    }
    protected void DocumentManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Document.aspx");
    }
    protected void DownloadManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Download.aspx");
    }
    protected void PasswordManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Password.aspx");
    }
    protected void CollegeConditionManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_CollegeCondition.aspx");
    }
    protected void LawManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Law.aspx");
    }
    protected void MeetingManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Meeting.aspx");
    }
    protected void OrgnizationManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Orgnization.aspx");
    }
    protected void RuleManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Rule.aspx");
    }
    protected void LinkManagement_Click(object sender, EventArgs e)
    {
        Response.Redirect("Admin_Link.aspx");
    }
    protected void Submit_Button_Click(object sender, EventArgs e)
    {
        switch (Convert.ToString(Session["NewsType"]))
        {
            case "Notice":
                if (Session["OperationType"].ToString().Equals("New"))
                {
                    OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();

                    var category = from cate in dc.News_Categories
                                   where cate.Category_name.Equals("重要通知")
                                   select new
                                   {
                                       标识 = cate.Category_id
                                   };

                    New news = new New();
                    news.Title = Request.Form["title"];
                    news.Content = Request.Form["content"];
                    news.Time = DateTime.Now;
                    news.News_category = category.ToArray().ElementAt(0).标识;
                    try
                    {
                        SQL.SQLHelper.Insert(news);
                    }
                    catch (Exception ex)
                    {
                    }
                    dc.Dispose();
                    title.Value = Request.Form["title"];
                    content.Value = Request.Form["content"];
                }
                else if (Session["OperationType"].ToString().Equals("Edit"))
                {
                    OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
                    var News = from news in dc.News
                               where news.News_id.Equals(Convert.ToInt32(Session["News_id"]))
                               select news;
                    News.ToArray().ElementAt(0).Title = Request.Form["title"];
                    News.ToArray().ElementAt(0).Content = Request.Form["content"];

                    title.Value = Request.Form["title"];
                    content.Value = Request.Form["content"];
                    dc.SubmitChanges();
                    dc.Dispose();
                }
                break;
            case "CollegeCondition":
                if (Session["OperationType"].ToString().Equals("New"))
                {
                    OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();

                    var category = from cate in dc.News_Categories
                                   where cate.Category_name.Equals("校情统计")
                                   select new
                                   {
                                       标识 = cate.Category_id
                                   };

                    New news = new New();
                    news.Title = Request.Form["title"];
                    news.Content = Request.Form["content"];
                    news.Time = DateTime.Now;
                    news.News_category = category.ToArray().ElementAt(0).标识;
                    try
                    {
                        SQL.SQLHelper.Insert(news);
                    }
                    catch (Exception ex)
                    {
                    }
                    dc.Dispose();
                    title.Value = Request.Form["title"];
                    content.Value = Request.Form["content"];
                }
                else if (Session["OperationType"].ToString().Equals("Edit"))
                {
                    OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
                    var News = from news in dc.News
                               where news.News_id.Equals(Convert.ToInt32(Session["News_id"]))
                               select news;
                    News.ToArray().ElementAt(0).Title = Request.Form["title"];
                    News.ToArray().ElementAt(0).Content = Request.Form["content"];

                    title.Value = Request.Form["title"];
                    content.Value = Request.Form["content"];
                    dc.SubmitChanges();
                    dc.Dispose();
                }
                break;
            case "Law":
                if (Session["OperationType"].ToString().Equals("New"))
                {
                    OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();

                    var category = from cate in dc.News_Categories
                                   where cate.Category_name.Equals("政策法规")
                                   select new
                                   {
                                       标识 = cate.Category_id
                                   };

                    New news = new New();
                    news.Title = Request.Form["title"];
                    news.Content = Request.Form["content"];
                    news.Time = DateTime.Now;
                    news.News_category = category.ToArray().ElementAt(0).标识;
                    try
                    {
                        SQL.SQLHelper.Insert(news);
                    }
                    catch (Exception ex)
                    {
                    }
                    dc.Dispose();
                    title.Value = Request.Form["title"];
                    content.Value = Request.Form["content"];
                }
                else if (Session["OperationType"].ToString().Equals("Edit"))
                {
                    OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
                    var News = from news in dc.News
                               where news.News_id.Equals(Convert.ToInt32(Session["News_id"]))
                               select news;
                    News.ToArray().ElementAt(0).Title = Request.Form["title"];
                    News.ToArray().ElementAt(0).Content = Request.Form["content"];

                    title.Value = Request.Form["title"];
                    content.Value = Request.Form["content"];
                    dc.SubmitChanges();
                    dc.Dispose();
                }
                break;
            case "Meeting":
                if (Session["OperationType"].ToString().Equals("New"))
                {
                    OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();

                    var category = from cate in dc.News_Categories
                                   where cate.Category_name.Equals("一周会议")
                                   select new
                                   {
                                       标识 = cate.Category_id
                                   };

                    New news = new New();
                    news.Title = Request.Form["title"];
                    news.Content = Request.Form["content"];
                    news.Time = DateTime.Now;
                    news.News_category = category.ToArray().ElementAt(0).标识;
                    try
                    {
                        SQL.SQLHelper.Insert(news);
                    }
                    catch (Exception ex)
                    {
                    }
                    dc.Dispose();
                    title.Value = Request.Form["title"];
                    content.Value = Request.Form["content"];
                }
                else if (Session["OperationType"].ToString().Equals("Edit"))
                {
                    OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
                    var News = from news in dc.News
                               where news.News_id.Equals(Convert.ToInt32(Session["News_id"]))
                               select news;
                    News.ToArray().ElementAt(0).Title = Request.Form["title"];
                    News.ToArray().ElementAt(0).Content = Request.Form["content"];

                    title.Value = Request.Form["title"];
                    content.Value = Request.Form["content"];
                    dc.SubmitChanges();
                    dc.Dispose();
                }
                break;
            case "Orgnization":
                if (Session["OperationType"].ToString().Equals("New"))
                {
                    OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();

                    var category = from cate in dc.News_Categories
                                   where cate.Category_name.Equals("组织机构")
                                   select new
                                   {
                                       标识 = cate.Category_id
                                   };

                    New news = new New();
                    news.Title = Request.Form["title"];
                    news.Content = Request.Form["content"];
                    news.Time = DateTime.Now;
                    news.News_category = category.ToArray().ElementAt(0).标识;
                    try
                    {
                        SQL.SQLHelper.Insert(news);
                    }
                    catch (Exception ex)
                    {
                    }
                    dc.Dispose();
                    title.Value = Request.Form["title"];
                    content.Value = Request.Form["content"];
                }
                else if (Session["OperationType"].ToString().Equals("Edit"))
                {
                    OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
                    var News = from news in dc.News
                               where news.News_id.Equals(Convert.ToInt32(Session["News_id"]))
                               select news;
                    News.ToArray().ElementAt(0).Title = Request.Form["title"];
                    News.ToArray().ElementAt(0).Content = Request.Form["content"];

                    title.Value = Request.Form["title"];
                    content.Value = Request.Form["content"];
                    dc.SubmitChanges();
                    dc.Dispose();
                }
                break;
            case "Rule":
                if (Session["OperationType"].ToString().Equals("New"))
                {
                    OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();

                    var category = from cate in dc.News_Categories
                                   where cate.Category_name.Equals("规章制度")
                                   select new
                                   {
                                       标识 = cate.Category_id
                                   };

                    New news = new New();
                    news.Title = Request.Form["title"];
                    news.Content = Request.Form["content"];
                    news.Time = DateTime.Now;
                    news.News_category = category.ToArray().ElementAt(0).标识;
                    try
                    {
                        SQL.SQLHelper.Insert(news);
                    }
                    catch (Exception ex)
                    {
                    }
                    dc.Dispose();
                    title.Value = Request.Form["title"];
                    content.Value = Request.Form["content"];
                }
                else if (Session["OperationType"].ToString().Equals("Edit"))
                {
                    OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
                    var News = from news in dc.News
                               where news.News_id.Equals(Convert.ToInt32(Session["News_id"]))
                               select news;
                    News.ToArray().ElementAt(0).Title = Request.Form["title"];
                    News.ToArray().ElementAt(0).Content = Request.Form["content"];

                    title.Value = Request.Form["title"];
                    content.Value = Request.Form["content"];
                    dc.SubmitChanges();
                    dc.Dispose();
                }
                break;
            case "Document":
                if (Session["OperationType"].ToString().Equals("New"))
                {
                    OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();

                    Document document = new Document();
                    document.Title = Request.Form["title"];
                    document.Content = Request.Form["content"];
                    document.Time = DateTime.Now;
                    
                    var Category_id = from document_category in dc.Document_Categories
                                      where document_category.Category_name.Equals(DocumenCategory.SelectedItem.Text)
                                      select new
                                      {
                                          category_id = document_category.Category_id
                                      };
                    int sign = 0;
                    foreach (var id in Category_id)
                    {
                       if(sign == 0)
                        {
                            document.Document_category = id.category_id;
                            sign = 1;
                            break;
                        }
                    }
                                        
                    try
                    {
                        SQL.SQLHelper.Insert(document);
                    }
                    catch (Exception ex)
                    {
                    }
                    dc.Dispose();
                    title.Value = Request.Form["title"];
                    content.Value = Request.Form["content"];
                }
                else if (Session["OperationType"].ToString().Equals("Edit"))
                {
                    OfficeDataClassesDataContext dc = new OfficeDataClassesDataContext();
                    var document = from doc in dc.Documents
                                   where doc.Document_id.Equals(Convert.ToInt32(Session["News_id"]))
                                   select doc;
                    document.ToArray().ElementAt(0).Title = Request.Form["title"];
                    document.ToArray().ElementAt(0).Content = Request.Form["content"];

                    title.Value = Request.Form["title"];
                    content.Value = Request.Form["content"];
                    dc.SubmitChanges();
                    dc.Dispose();
                }
                break;
        }
    }
    protected void Return_Button_Click(object sender, EventArgs e)
    {
        this.RegisterClientScriptBlock("E", "<script language=javascript>history.go(-2);</script>");
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        
    }
    protected void SqlDocumentCategory_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

    }
}
